package com.ysd.lis.service.sta.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.sta.StaMenuDto;
import com.ysd.lis.dto.sta.StaMenuParam;
import com.ysd.lis.entity.SysMenu;
import com.ysd.lis.entity.sta.StaMenu;
import com.ysd.lis.mapper.sta.StaMenuMapper;
import com.ysd.lis.mapper.sys.SysMenuMapper;
import com.ysd.lis.service.sta.StaMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.sys_module 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-12-27
 */
@Service
public class StaMenuServiceImpl extends ServiceImpl<StaMenuMapper, StaMenu> implements StaMenuService {

    @Autowired
    StaMenuMapper staMenuMapper;

    @Override
    public Result findStaMenuList(StaMenuParam pageParam) {
        MPJLambdaWrapper<StaMenu> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(StaMenu::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(StaMenu::getStaMenuCode, pageParam.getSeachValue())
                    .or().like(StaMenu::getMenuCode, pageParam.getSeachValue())
                    .or().like(StaMenu::getMenuTitle,pageParam.getSeachValue()));
        }

        lambdaQueryWrapper.orderByAsc(StaMenu::getSeq);
        List<StaMenu> supplierPage = staMenuMapper.selectList( lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result addStaMenu(StaMenu staMenu) {
        int insert = staMenuMapper.insert(staMenu);
        if(insert>0){
            return Result.succ(1,"添加成功",staMenu);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editStaMenu(StaMenu supplier) {
        int updateById = staMenuMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result batchDeleteStaMenuList(StaMenuDto dto) {
        List<StaMenu> supplierList = dto.getStaMenuList();
        List<String> ids = supplierList.stream().map(StaMenu::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }

    @Autowired
    SysMenuMapper sysMenuMapper;
    @Override
    public Result sysStaMenu(StaMenuParam param) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getDelFlag,0);
        queryWrapper.isNotNull(SysMenu::getParentId);
        queryWrapper.ne(SysMenu::getParentId,"");
        List<SysMenu> menuList = sysMenuMapper.selectList(queryWrapper);
        //复制menulist到staMenuList
        List<StaMenu> addStaMenuList = new ArrayList<>();
        for(SysMenu sysMenu:menuList){
            StaMenu staMenu = new StaMenu();
            BeanUtil.copyProperties(sysMenu, staMenu, "id","parentId","moduleId" ,"creator", "editor", "createTime", "editTime", "delFlag");
            staMenu.setStaMenuCode(sysMenu.getCode());
            staMenu.setMenuCode(sysMenu.getCode());
            staMenu.setMenuTitle(sysMenu.getTitle());
            staMenu.setSeq(sysMenu.getSn());
            staMenu.setDescription(sysMenu.getTitle());
            addStaMenuList.add(staMenu);
        }
        this.saveBatch(addStaMenuList);
        return Result.succ(1, "成功", null);
    }

    @Override
    public Result findStaMenuPageList(StaMenuParam pageParam) {
        Page<StaMenu> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        MPJLambdaWrapper<StaMenu> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(StaMenu::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(StaMenu::getStaMenuCode, pageParam.getSeachValue())
                    .or().like(StaMenu::getMenuCode, pageParam.getSeachValue())
                    .or().like(StaMenu::getMenuTitle,pageParam.getSeachValue()));
        }

        lambdaQueryWrapper.orderByAsc(StaMenu::getMenuCode);
        Page<StaMenu> supplierPage = staMenuMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }
}
